<template>
  <a-modal
    :open="open"
    width="640px"
    :title="formState?.fjlxbh ? '修改床位' : '新增床位'"
    centered
    :keyboard="false"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" :colon="false" class="form-style-flex">
      <a-form-item label="病区名称" class="w-full">
        {{ formState.wardName }}
      </a-form-item>
      <a-form-item label="床位号" class="w-1/2" name="bedCode" v-bind="validateInfos.bedCode">
        <a-input v-model:value="formState.bedCode" :disabled="props.rowData.bedCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="房间" class="w-1/2" name="bedCode">
        <!-- <a-input v-model:value="formState.bedCode" :disabled="props.rowData.bedCode" placeholder="请输入" /> -->
        <a-select v-model:value="formState.roomId" :options="roomData" placeholder="请选择" :field-names="{ label: 'roomName', value: 'roomId' }" allow-clear />
      </a-form-item>
      <a-form-item label="关联床位费" class="w-1/2">
        <SelectTable v-model:value="formState.sfxmbName" :table-width="500" :search-params="{ xmlb: ['床位费'] }" type="关联项目" @select="onSelectProject" />
      </a-form-item>
      <a-form-item class="w-[220px] pl-5">
        <div>床位单价：￥{{ formState.cwf || '-' }}</div>
      </a-form-item>
      <a-form-item>
        <a-checkbox v-model:checked="formState.isExtraBed">加床</a-checkbox>
      </a-form-item>
      <a-form-item label="关联诊查费" class="w-1/2">
        <SelectTable v-model:value="formState.zlfName" :table-width="500" :search-params="{ xmlb: ['诊疗费'] }" type="关联项目" @select="onSelectDiagnosis" />
      </a-form-item>
      <a-form-item class="w-[220px] pl-5">
        <div>诊查单价：￥{{ formState.zlf || '-' }}</div>
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/2" name="bedCode">
        <a-input-number v-model:value="formState.outOrder" class="w-full" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" placeholder="请输入" :rows="5" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const emit = defineEmits(['update:value', 'confirm', 'success'])
const roomData = ref([])
const formState = ref({
  isExtraBed: false,
  bedCode: ''
})
const rulesState = ref({
  bedCode: [{ required: true, message: '请输入床位号' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

// 房间
const getRoomData = async () => {
  const { code, data, msg } = await post('/operation/opex/getDictWardRoomList', { wardId: props.rowData.wardId })
  if (code !== 0) return message.error(msg)
  roomData.value = data
}

const onSelectProject = (row) => {
  formState.value.sfxmbBh = row.bh
  formState.value.sfxmbName = row.mc
  formState.value.cwf = row.dj ? row.dj.toFixed(2) : ''
}
const onSelectDiagnosis = (row) => {
  formState.value.sfxmbDiagnosisBh = row.bh
  formState.value.zlfName = row.mc
  formState.value.zlf = row.dj ? row.dj.toFixed(2) : ''
}
const onOk = async () => {
  const { code, msg } = await post('/operation/opex/doSaveBedInfo', {
    ...formState.value,
    isExtraBed: formState.value.isExtraBed ? 1 : 0,
    status: props.rowData.status ? props.rowData.status : 0
  })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('success')
}
const onCancel = () => {
  resetFields()
  emit('confirm')
}

watch(
  () => formState.value.sfxmbName,
  (val) => {
    if (!val) {
      formState.value.cwf = ''
    }
  }
)

watch(
  () => props.open,
  async (val) => {
    if (val) {
      await getRoomData()
      formState.value = { ...props.rowData }
      formState.value.isExtraBed = +props.rowData.isExtraBed ? true : false
      formState.value.cwf = props.rowData.cwf && Number(props.rowData.cwf).toFixed(2)
      formState.value.zlf = props.rowData.zlf && Number(props.rowData.zlf).toFixed(2)
    }
  },
  {
    immediate: true
  }
)
</script>
<style lang="less" scoped>
.ant-form {
  display: flex;
  flex-flow: row wrap;
  min-width: 0;
  :deep(.ant-form-item) {
    .ant-form-item-label {
      width: 90px;
    }
  }
  .ant-picker {
    width: 100%;
  }
}
</style>
